package com.todod.base.module.base.controller.admin.devicebase;

import com.todod.base.module.base.controller.admin.dto.ReportDataReq;
import com.todod.base.module.base.controller.admin.dto.ResultData;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.devicebase.vo.*;
import com.todod.base.module.base.dal.dataobject.devicebase.DeviceBaseDO;
import com.todod.base.module.base.service.devicebase.DeviceBaseService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 仪器型号查看")
@RestController
@RequestMapping("/base/device-base")
@Validated
public class DeviceBaseController {

    @Resource
    private DeviceBaseService deviceBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建仪器型号查看")
    public CommonResult<Long> createDeviceBase(@Valid @RequestBody DeviceBaseSaveReqVO createReqVO) {
        return success(deviceBaseService.createDeviceBase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仪器型号查看")
    public CommonResult<Boolean> updateDeviceBase(@Valid @RequestBody DeviceBaseSaveReqVO updateReqVO) {
        deviceBaseService.updateDeviceBase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仪器型号查看")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDeviceBase(@RequestParam("id") Long id) {
        deviceBaseService.deleteDeviceBase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仪器型号查看")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DeviceBaseRespVO> getDeviceBase(@RequestParam("id") Long id) {
        DeviceBaseDO deviceBase = deviceBaseService.getDeviceBase(id);
        return success(BeanUtils.toBean(deviceBase, DeviceBaseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仪器型号查看分页")
    public CommonResult<PageResult<DeviceBaseRespVO>> getDeviceBasePage(@Valid DeviceBasePageReqVO pageReqVO) {
        PageResult<DeviceBaseDO> pageResult = deviceBaseService.getDeviceBasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceBaseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仪器型号查看 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceBaseExcel(@Valid DeviceBasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceBaseDO> list = deviceBaseService.getDeviceBasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仪器型号查看.xls", "数据", DeviceBaseRespVO.class,
                        BeanUtils.toBean(list, DeviceBaseRespVO.class));
    }

    /**
     * 获取基地仪器清单列表信息
     **/
    @GetMapping("/getlist")
    @Operation(summary = "获取仪器型号列表信息", description = "主要用于前端的下拉选项")
    public CommonResult<List<DeviceBaseRespVO>> getListSite(DeviceBaseSaveReqVO reqVO){
        return success(BeanUtils.toBean(deviceBaseService.getListDevice(reqVO),DeviceBaseRespVO.class));
    }



    @GetMapping(value = "/downTemplate")
    @Operation(summary = "下载导入模版")
    public void problemTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        deviceBaseService.problemTemplate(request, response);
    }
    @PostMapping(value = "/uploadFile")
    @Operation(summary = "上传导入文件")
    public ResultData uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        return deviceBaseService.uploadFile(file);
    }
    @PostMapping(value = "/reportDepartment")
    @Operation(summary = "执行导入文件")
    public ResultData reportDepartment(@RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        return deviceBaseService.reportDepartment(reportDataReq);
    }
    @PostMapping(value = "/downloadWrongReport")
    @Operation(summary = "下载导入失败错误报告")
    public void downloadWrongReport(HttpServletResponse response, @RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        deviceBaseService.downloadWrongReport(response, reportDataReq.getFileUrl());
    }


}